package com.weike.yuanding.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.weike.yuanding.constant.UserVideoRelationEnum;
import com.weike.yuanding.core.domain.R;
import com.weike.yuanding.dto.user.UserDTO;
import com.weike.yuanding.entity.VideoMainEntity;
import com.weike.yuanding.mapper.UserVideoRelationMapper;
import com.weike.yuanding.mapper.VideoMainMapper;
import com.weike.yuanding.utils.BaseContext;
import com.weike.yuanding.vo.VideoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @ClassName: UserVideoRelationController
 * @Author: YuanDing
 * @Date: 2025/4/10 22:00
 * @Description:
 */

@RequestMapping("video/user")
@RestController
public class UserVideoRelationController {

    @Autowired
    private UserVideoRelationMapper userVideoRelationMapper;

    @Autowired
    private VideoMainMapper videoMainMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @GetMapping("info")
    public R userInfo() {
        String userId = BaseContext.userId.get();
        UserDTO userDTO = new UserDTO();
        HashMap<String, Object> searchMap = new HashMap<>();
        searchMap.put("userId" , userId);
        searchMap.put("type" , UserVideoRelationEnum.LIKE.getCode());
        userDTO.setLikedNumber(userVideoRelationMapper.selectNumber(searchMap));

        return R.ok(userDTO);
    }

    @PostMapping("videoList")
    public R videoList(@RequestBody VideoVo videoVo) {
        String userId = videoVo.getUserId();
        // 1 - 作品 2 - 喜欢 3 - 收藏 4 - 观看历史
        HashMap<String, Object> searchMap = new HashMap<>();
        searchMap.put("userId" , userId);
        if (videoVo.getType() == 1) {
            return R.ok(videoMainMapper.selectList(new LambdaQueryWrapper<VideoMainEntity>()
                    .eq(VideoMainEntity::getCreateUser , userId)));

        } else if (videoVo.getType() == 2) {
            searchMap.put("type" , UserVideoRelationEnum.LIKE.getCode());
            return R.ok(videoMainMapper.selectVideoListByUserId(searchMap));
        } else if (videoVo.getType() == 3) {
            searchMap.put("type" , UserVideoRelationEnum.SHOU_CANG.getCode());
            return R.ok(videoMainMapper.selectVideoListByUserId(searchMap));
        } else if (videoVo.getType() == 4) {

            Set<String> idSet = redisTemplate.opsForSet().members("user:video:viewed:" + userId);

            if (idSet == null || idSet.isEmpty()) {
                return R.ok();
            }
            List<String> idList = new ArrayList<>(idSet);
            List<VideoMainEntity> videoMainEntities = videoMainMapper.selectBatchIds(idList.stream()
                    .sorted(Collections.reverseOrder())
                    .collect(Collectors.toList()));


            return R.ok(videoMainEntities);
        }

        return R.fail();
    }

}
